//
//  NetWorkRequest.m
//  MyBus
//
//  Created by yytmzys on 15/3/16.
//  Copyright (c) 2015年 Beijing Fengyangtianshun Technology Co., Ltd. All rights reserved.
//

#import "NetWorkRequest.h"
#import "AFNetworking.h"
#import "YWStaticPublicHeader.h"
@implementation NetWorkRequest

/// 1.检测网络状态
- (void)netWorkStatus
{
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
    {
        NSLog(@"%ld", status);
    }];
}
#pragma mark --private Method--数据请求
+ (void)netWorkPOSTWithURLString:(NSString *)urlString parameters:(NSDictionary*)parameters SuccessBlock:(void(^)(NSDictionary*dic))successBlock failBlock:(void(^)(NSError*error))failBlock{
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        manager.requestSerializer=[AFJSONRequestSerializer  serializer];
        manager.requestSerializer.timeoutInterval = 10.f;
    
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", nil];

        NSString *url = [NSString stringWithFormat:@"%@%@", HTTP_BaseURL, urlString];
        NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:parameters];
        [manager POST:url parameters:mDic progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
        {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         if (successBlock)
          {
            successBlock(dictionary);
                    
          }
        
    }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failBlock)
        {
            failBlock(error);
        }
        
    }];
    
}
@end
